/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.collablet.coop.recommend;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import br.org.groupwareworkbench.collablet.coop.recommend.XmlRecommendConfig;
import br.org.groupware_workbench.commonalgorithms.analisadortextual_1_1.AnalisadorTextual;
import br.org.groupware_workbench.commonalgorithms.analisadortextual_1_1.com.alag.ci.textanalysis.TagMagnitudeVector;
import br.org.groupware_workbench.commonalgorithms.analisadortextual_1_1.exception.AnalisadorTextualException;
import br.org.groupwareworkbench.core.bd.AssignedTo;
import br.org.groupwareworkbench.core.bd.GenericEntity;

@Entity
@Table(name="gw_collab_Recommend")
public class Recommend extends GenericEntity {

    private static final long serialVersionUID = -3399306816323250833L;

    @ManyToOne
    @AssignedTo
    private GenericEntity genericEntityFrom;

    @ManyToOne
    @AssignedTo
    private GenericEntity genericEntityTo;

    private double similaritysProduct;

    public Recommend() {
    }

    public Recommend(XmlRecommendConfig xmlConfig, GenericEntity genericEntityFrom, GenericEntity genericEntityTo) {
        this.genericEntityFrom = genericEntityFrom;
        this.genericEntityTo = genericEntityTo;
        this.similaritysProduct = getSimilarity(xmlConfig, genericEntityFrom, genericEntityTo);
    }

    public GenericEntity getGenericEntityFrom() {
        return genericEntityFrom;
    }

    public void setGenericEntityFrom(GenericEntity genericEntityFrom) {
        this.genericEntityFrom = genericEntityFrom;
    }

    public GenericEntity getGenericEntityTo() {
        return genericEntityTo;
    }

    public void setGenericEntityTo(GenericEntity genericEntityTo) {
        this.genericEntityTo = genericEntityTo;
    }

    public double getSimilaritysProduct() {
        return similaritysProduct;
    }

    public void setSimilaritysProduct(double similaritysProduct) {
        this.similaritysProduct = similaritysProduct;
    }

    private static TagMagnitudeVector getTagVector(XmlRecommendConfig xmlConfig, GenericEntity entity) {
        try {
            String text = "";
            List<String> texts= xmlConfig.extractMethods(entity);
            for (String string : texts) {
                text += string;
            }
            return new AnalisadorTextual().analisar(text);
        } catch (AnalisadorTextualException e) {
            throw new RuntimeException(e);
        }
    }

    private double getSimilarity(XmlRecommendConfig xmlConfig, GenericEntity genericEntityFrom, GenericEntity genericEntityTo) {
        TagMagnitudeVector geTamVectorFrom = getTagVector(xmlConfig, genericEntityFrom);
        TagMagnitudeVector gerTamVectorTo = getTagVector(xmlConfig, genericEntityTo);
        return geTamVectorFrom.dotProduct(gerTamVectorTo);
    }
}